package com.xci.platform.auth.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.hutool.core.date.DateUtil;
import com.xci.platform.core.Constant;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * 系统日志
 */
@Data
public class LogEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 日志主键
     */
    @NotBlank(message = "日志主键不能为空")
    @Size(min = 1, max = 50, message = "日志主键不能超过50个字符")
    @ExcelIgnore
    private String id;

    /**
     * 操作用户主键
     */
    @ExcelIgnore
    private String operateUserId;

    /**
     * 操作用户
     */
    @Excel(name = "操作用户")
    private String operateUserName;

    /**
     * 操作日期
     */
    @Excel(name = "操作日期", width = 20,exportFormat = Constant.DefaultDateTimePattern)
    private Date operateDateTime;

    /**
     * 操作类型 1: 登录 2: 操作 3: 授权 4: 访问 5: 异常
     */
    @NotNull(message = "操作类型不能为空")
    @ExcelIgnore
    // @Excel(name = "操作类型", replace = {"登录_1", "操作_2", "授权_3", "访问_4", "异常_5"})
    private Integer operateType;

    /**
     * 操作类型
     */
    @Excel(name = "操作类型")
    private String operateTypeText;

    /**
     * 模块
     */
    @Size(min = 0, max = 50, message = "模块不能超过50个字符")
    @Excel(name = "模块")
    private String module;

    /**
     * IP地址
     */
    @Size(min = 0, max = 50, message = "IP地址不能超过50个字符")
    @Excel(name = "IP地址", width = 15)
    private String ipAddress;

    /**
     * 浏览器
     */
    @Size(min = 0, max = 50, message = "浏览器不能超过50个字符")
    @Excel(name = "浏览器", width =20)
    private String browser;

    /**
     * 请求地址
     */
    @Size(min = 0, max = 50, message = "请求地址不能超过50个字符")
    @Excel(name = "请求地址", width =20)
    private String url;

    /**
     * 耗时
     */
    @ExcelIgnore
    private Long costTime;

    /**
     * 耗时文本
     */
    @Excel(name = "耗时", width =15)
    private String costTimeText;

    /**
     * 执行状态
     */
    @Excel(name = "执行状态",replace = {"成功_1", "失败_2"})
    private Integer result;

    /**
     * 执行信息
     */
    @Size(min = 0, max = 4000, message = "执行信息不能超过4000个字符")
    @Excel(name = "执行信息", width = 30)
    private String message;

    /**
     * 详细信息
     */
    @Excel(name = "详细信息", width = 30)
    private String details;

    public String getCostTimeText() {
        if (costTime != null && costTime > 0)
            return DateUtil.formatBetween(costTime);
        return Constant.Empty;
    }

    public String getOperateTypeText() {
        //操作类型 1: 登录 2: 操作 3: 授权 4: 访问 5: 异常
        switch (operateType) {
            case 1:
                return "登录";
            case 2:
                return "操作";
            case 3:
                return "授权";
            case 4:
                return "访问";
            case 5:
                return "异常";
        }
        return Constant.Empty;
    }
}
